package com.csoa.community.adapter;

import java.util.List;

import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.collect.community.R;
import com.csoa.community.activity.LocalSpeciesTableActivity;
import com.csoa.community.activity.SpeciesInfoActivity;
import com.csoa.community.bean.LocalSpeciesBean;

public class LocalSpeciesAdapter extends BaseAdapter {

	private LocalSpeciesTableActivity context;
	private LayoutInflater mInflater;
	private List<LocalSpeciesBean> mItemList;

	public LocalSpeciesAdapter(LocalSpeciesTableActivity context, List<LocalSpeciesBean> lists) {
		super();
		this.context = context;
		this.mItemList = lists;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return mItemList == null ? 0 : mItemList.size();
	}

	@Override
	public LocalSpeciesBean getItem(int arg0) {
		// TODO Auto-generated method stub
		return mItemList == null ? null : mItemList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int index, View view, ViewGroup arg2) {
		View tempItemView = null;
		ViewHolder viewHolder = null;

		if (view == null) {
			tempItemView = mInflater.inflate(R.layout.list_item_species, null);
			viewHolder = new ViewHolder();
			viewHolder.latinName = (TextView) tempItemView
					.findViewById(R.id.tvLatinName);
			viewHolder.chineseName = (TextView) tempItemView
					.findViewById(R.id.tvChineseName);
			viewHolder.ke = (TextView) tempItemView
					.findViewById(R.id.tvKe);
			viewHolder.zhong = (TextView) tempItemView.findViewById(R.id.tvZhong);
			viewHolder.shu = (TextView) tempItemView.findViewById(R.id.tvShu);
			viewHolder.area = (TextView) tempItemView.findViewById(R.id.tvArea);
			tempItemView.setTag(viewHolder);
		} else {
			tempItemView = view;
			viewHolder = (ViewHolder) tempItemView.getTag();
		}

		if (mItemList.size() > 1) {
			if (index == 0) {
				tempItemView
						.setBackgroundResource(R.drawable.background_view_rounded_top);
			} else if (index == mItemList.size() - 1) {
				tempItemView
						.setBackgroundResource(R.drawable.background_view_rounded_bottom);
			} else {
				tempItemView
						.setBackgroundResource(R.drawable.background_view_rounded_middle);
			}

		} else if (mItemList.size() == 1) {
			tempItemView
					.setBackgroundResource(R.drawable.background_view_rounded_single);
		}

		LocalSpeciesBean wzInfo = getItem(index);
		viewHolder.latinName.setText(Html.fromHtml("<html><font color='red'>拉丁学名:</font></html>") + wzInfo.SName);
		viewHolder.chineseName.setText("中文名:" + wzInfo.Chinese);
		viewHolder.area.setText("分布区域:" +  wzInfo.Country);
		viewHolder.ke.setText("科:" + wzInfo.Family);
		viewHolder.shu.setText("属:" + wzInfo.Genus);
		viewHolder.zhong.setText("种:" + wzInfo.Sp1);

		tempItemView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(index);
			}
		});

		return tempItemView;
	}

	private static final class ViewHolder {
		TextView latinName;
		TextView chineseName;
		TextView ke;
		TextView shu;
		TextView zhong;
		TextView area;
	}


	
	public void startActivity(int position) {
		Intent intent = new Intent(context, SpeciesInfoActivity.class);
		intent.putExtra("SpeciesInfo", mItemList.get(position));
		context.startActivityForResult(intent,0);
	}

}
